package com.omega.encryptedsql.controller;

import com.fasterxml.jackson.databind.util.BeanUtil;
import com.omega.encryptedsql.dto.EmployeeDto;
import com.omega.encryptedsql.po.Employee;
import com.omega.encryptedsql.service.EmployeeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author omega
 * @description: TODO
 * @date 2025/7/10
 */
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @GetMapping("/{id}")
    public EmployeeDto getById(@PathVariable Long id) {
        Employee employee = employeeService.getEmployeeById(id);
        EmployeeDto employeeDto = new EmployeeDto();
        employeeDto.setId(employee.getId());
        employeeDto.setName(employee.getName());
        employeeDto.setAge(employee.getAge());
        employeeDto.setEmail(employee.getEmail());
        employeeDto.setPhone(employee.getPhone());
        employeeDto.setCreateTime(employee.getCreateTime());
        employeeDto.setUpdateTime(employee.getUpdateTime());
        return employeeDto;
    }

    @PostMapping("/saveEmployee")
    public boolean save(@RequestBody EmployeeDto employeeDto) {
        Employee employee = new Employee();
        employee.setName(employeeDto.getName());
        employee.setAge(employeeDto.getAge());
        employee.setEmail(employeeDto.getEmail());
        employee.setPhone(employeeDto.getPhone());
        employee.setCreateTime(employeeDto.getCreateTime());
        employee.setUpdateTime(employeeDto.getUpdateTime());
        return employeeService.save(employee);
    }

    @PostMapping("/batchSaveEmployees")
    public boolean batchSaveEmployees(@RequestBody List<EmployeeDto> employeeDtos) {
        Employee employee;
        List<Employee> employees = new ArrayList<>();
        for (EmployeeDto employeeDto : employeeDtos) {
            employee = new Employee();
            employee.setName(employeeDto.getName());
            employee.setAge(employeeDto.getAge());
            employee.setEmail(employeeDto.getEmail());
            employee.setPhone(employeeDto.getPhone());
            employee.setCreateTime(employeeDto.getCreateTime());
            employee.setUpdateTime(employeeDto.getUpdateTime());
            employees.add(employee);
        }

        return employeeService.saveBatch(employees);
    }

    @PostMapping("/queryWithParameters")
    public List<EmployeeDto> queryWithParameters(@RequestBody EmployeeDto employeeDto) {
        return null;
    }

    @PostMapping("listAll")
    public List<Employee> listAll(@RequestBody Employee employee) {
        return employeeService.list();
    }

    @PostMapping("/updateEmployee")
    public boolean update(@RequestBody EmployeeDto employeeDto) {
        Employee employee = new Employee();
        employee.setId(employeeDto.getId());
        employee.setName(employeeDto.getName());
        employee.setAge(employeeDto.getAge());
        employee.setEmail(employeeDto.getEmail());
        employee.setPhone(employeeDto.getPhone());
        employee.setCreateTime(employeeDto.getCreateTime());
        employee.setUpdateTime(employeeDto.getUpdateTime());
        return employeeService.updateById(employee);
    }

    @GetMapping("/searchWithName/{name}")
    public List<EmployeeDto> searchWithName(@PathVariable String name) {
        List<Employee> employees = employeeService.searchWithName(name);
        EmployeeDto employeeDto;
        List<EmployeeDto> employeeDtos = new ArrayList<>();
        for (Employee employee : employees) {
            employeeDto = new EmployeeDto();
            employeeDto.setId(employee.getId());
            employeeDto.setName(employee.getName());
            employeeDto.setAge(employee.getAge());
            employeeDto.setEmail(employee.getEmail());
            employeeDto.setPhone(employee.getPhone());
            employeeDto.setCreateTime(employee.getCreateTime());
            employeeDto.setUpdateTime(employee.getUpdateTime());
            employeeDtos.add(employeeDto);
        }
        return employeeDtos;
    }

    @GetMapping("/searchWithNameNoLambda/{name}")
    public List<EmployeeDto> searchWithNameNoLambda(@PathVariable String name) {
        List<Employee> employees = employeeService.searchWithNameNoLambda(name);
        EmployeeDto employeeDto;
        List<EmployeeDto> employeeDtos = new ArrayList<>();
        for (Employee employee : employees) {
            employeeDto = new EmployeeDto();
            employeeDto.setId(employee.getId());
            employeeDto.setName(employee.getName());
            employeeDto.setAge(employee.getAge());
            employeeDto.setEmail(employee.getEmail());
            employeeDto.setPhone(employee.getPhone());
            employeeDto.setCreateTime(employee.getCreateTime());
            employeeDto.setUpdateTime(employee.getUpdateTime());
            employeeDtos.add(employeeDto);
        }
        return employeeDtos;
    }
}
